package com.mq.conf;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

@Configuration
public class DelayQueueConfig implements Ordered {

    //创建一个延时队列的名字
    public final static String DELAY_QUEUE ="delay_queue";
    //创建一个工作队列的名字
    public final static String PROCESS_QUEUE ="process_queue";
    //创建一个死信交换机的名字
    public final static String DELAY_EXCHANGE ="delay_exchange";
    //定义一个全局消息过期时间
    public final static int EXPIRE_TIME = 4000;

    //c创建一个延时队列
    @Bean
    Queue delayQueue(){
        return QueueBuilder.durable(DELAY_QUEUE)
                .withArgument("x-dead-letter-exchange",DELAY_EXCHANGE)
                .withArgument("x-dead-letter-routing-key",PROCESS_QUEUE).build();
    }
    //创建一个工作队列
    @Bean
    Queue processQueue(){
        return QueueBuilder.durable(PROCESS_QUEUE).build();
    }

    //创建一个死信交换机
    @Bean
    DirectExchange delayExchange(){
        return new DirectExchange(DELAY_EXCHANGE);
    }

    //完成工作队列和交换机的绑定
    @Bean
    Binding dalayExchangeBindQueue(Queue processQueue, DirectExchange delayExchange ){
        return BindingBuilder.bind(processQueue).to(delayExchange).with(PROCESS_QUEUE);
    }
    @Override
    public int getOrder() {
        return -100;
    }
}
